package org.vectrics.recruiter.struts.tag;

/**
 * Title: vectrics Server for Logging Description: Copyright: Copyright (c) 2001
 * Company: vectrics Software Inc. Service is Built In
 * 
 * @author Mike Moore
 * @version 1.0
 */

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Enumeration;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.log4j.Logger;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;

public class DebugTag extends TagSupport {

    private static final long serialVersionUID = 3422453999348811910L;

    static private transient Logger log = Logger.getLogger(DebugTag.class);

    private Properties roles = new Properties();

    private static SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");

    private String jobId = "";

    private int width = 400;

    private void showRequestParameters(final StringBuffer html, final HttpServletRequest request) {
        html.append("<h4>Request Parameters</h4>");
        Table table = new BorderTable();
        table.setWidth(600);
        Row hdrRow = table.addRow();
        hdrRow.addCell().addElement("subFont", "<b>Parameter</b>");
        hdrRow.addCell().addElement("subFont", "<b>Value</b>");
        Enumeration paramEnum = request.getParameterNames();
        while (paramEnum.hasMoreElements()) {
            String name = (String) paramEnum.nextElement();
            String value = request.getParameter(name);
            Row row = table.addRow();
            Cell cell = row.addCell();
            cell.addElement("<font>" + name + "</font>");
            cell = row.addCell();
            cell.addElement("<font>" + value + "</font>");
        }
        html.append(table.toString());
    }

    @Override
    public int doStartTag() throws JspException {
        log.info("doStartTag() called");
        StringBuffer html = new StringBuffer();
        HttpServletRequest request = (HttpServletRequest) super.pageContext.getRequest();
        HttpSession session = super.pageContext.getSession();

        html.append("<h2>Debug Information</h2>");
        showRequestParameters(html, request);
        showAttributes("Request", html, PageContext.REQUEST_SCOPE);
        showAttributes("Session", html, PageContext.SESSION_SCOPE);
        showAttributes("Page", html, PageContext.PAGE_SCOPE);
        html.append("<h3>Servlet Name = " + pageContext.getServletConfig().getServletName() + "</h3>");

        JspWriter writer = pageContext.getOut();
        try {
            writer.print(html.toString());
        } catch (IOException ex) {
            log.error("IOException attempting to write tag to output. ", ex);
            throw new JspException("IOException attempting to write tag to output. " + ex.getMessage());
        }
        return EVAL_PAGE;
    }

    private void showAttributes(final String scope, final StringBuffer html, final int scopeInt) {
        Enumeration attrEnum = pageContext.getAttributeNamesInScope(scopeInt);
        if (attrEnum.hasMoreElements()) {
            html.append("<h4>" + scope + " Attributes</h4>");
            Table table = new BorderTable();
            table.setWidth(600);
            Row hdrRow = table.addRow();
            hdrRow.addCell().addElement(TextHelper.getFont("subFont", "<b>Attribute Name</b>"));
            hdrRow.addCell().addElement(TextHelper.getFont("subFont", "<b>Class</b>"));
            hdrRow.addCell().addElement(TextHelper.getFont("subFont", "<b>Value</b>"));

            while (attrEnum.hasMoreElements()) {
                Row row = table.addRow();
                String attr = (String) attrEnum.nextElement();
                row.addCell().addElement(TextHelper.getFont("subFont", attr));

                log.debug("Getting attribute value object ...");

                Object attrObj = pageContext.getAttribute(attr, scopeInt);

                String value = "Exception finding value";
                try {
                    value = attrObj.toString();
                } catch (Exception ex) {
                    log.error("Exception finding value", ex);
                }

                String className = "Exception finding class";
                try {
                    className = attrObj.getClass().getName();
                } catch (Exception ex) {
                    log.error("Exception finding class", ex);
                }
                log.debug("Atribute Displaying...");
                row.addCell().addElement(TextHelper.getFont("subFont", className));
                row.addCell().addElement(TextHelper.getFont("subFont", value));

                // html.append("<p>Attribute:" + attr + "<br> Type: " +
                // className + "<br> Value: " + value + "</p>");
            }
            html.append(table.toString());
        } else {
            html.append("<h4>" + scope + "No Attributes</h4>");

        }
        log.debug("showAttributes() - done");

    }

    /**
     * @return
     */
    public int getWidth() {
        return width;
    }

    /**
     * @param width
     */
    public void setWidth(final int width) {
        this.width = width;
    }

}
